import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        screenWidth: document.documentElement.clientWidth || document.body.clientWidth ,// 初始化状
        weatherData: '',
        leftmenu: [],
        zoom:1
    },
    mutations: {
        setZoom(state, data) {
            state.zoom = data;
        },
        setLeftmenu(state, data) {
            state.leftmenu = data;
        },
        UPDATE_SCREEN_WIDTH(state, width) {
            state.screenWidth = width
          },
        setWeatherData(state, data) {
            state.weatherData = data;
        }
    },
    actions: {
        // 更新屏幕宽度的action，包含防抖逻辑:cite[2]:cite[10]
        updateScreenWidth: (function () {
          let timer = null
          return ({ commit }) => {
            if (timer) clearTimeout(timer)
            timer = setTimeout(() => {
              const width = document.documentElement.clientWidth || document.body.clientWidth
              commit('UPDATE_SCREEN_WIDTH', width)
            }, 300) // 防抖延迟300毫秒
          }
        })()
      },
    getters: {
        isMobile: state => {
            return state.screenWidth <= 768
          }
    }

})